//
//  NextChangePhoneViewController.m
//  CommunityApp
//
//  Created by 秦乾龙 on 16/12/16.
//  Copyright © 2016年 秦乾龙. All rights reserved.
//

#import "NextChangePhoneViewController.h"
#import "SetViewController.h"
#import "NextChangePhoneViewController.h"
#import "AFNetworking.h"//主要用于网络请求方法
#import "UIKit+AFNetworking.h"//里面有异步加载图片的方法
#import "AFHTTPSessionManager.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "NavView.h"

#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScale   ([UIScreen mainScreen].bounds.size.width)/750
@interface NextChangePhoneViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) NSString *codeStr;
@property (nonatomic,strong) UITextField *codeField;
@property (nonatomic,strong) UITextField *phoneField;

@end

@implementation NextChangePhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NavView *nav = [[NavView alloc]initWithTitle:@"修改手机号码" LeftImage:[ UIImage imageNamed:@"返回"]];
    nav.frame = CGRectMake(0, 20, kScreenW, 44);
    nav.backgroundColor = [UIColor colorWithRed:32/255.0 green:32/255.0 blue:32/255.0 alpha:1.0f];
    nav.titleLable.textColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor colorWithRed:32/255.0 green:32/255.0 blue:32/255.0 alpha:1.0f];
    [nav.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nav];
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, kScreenW, kScreenH-64)];
    backgroundView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0f];
    nav.rightBtn.hidden = NO;
    nav.rightBtn.tag = 10;
    [nav.rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    [nav.rightBtn addTarget:self action:@selector(saveClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backgroundView];
    UIView *phoneView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 100*kScale)];
    phoneView.backgroundColor = [UIColor whiteColor];
    [backgroundView addSubview:phoneView];

    _phoneField = [[UITextField alloc]init];
    _phoneField.delegate = self;
    _phoneField.font = [UIFont systemFontOfSize:15];
    _phoneField.placeholder = @"请输入新手机号码";
    _phoneField.frame = CGRectMake(30*kScale, 0, kScreenW-30*kScale, 100*kScale);
    [phoneView addSubview:_phoneField];
    UIView *codeView = [[UIView alloc]initWithFrame:CGRectMake(0, 102*kScale, kScreenW, 100*kScale)];
    codeView.backgroundColor = [UIColor whiteColor];
    [backgroundView addSubview:codeView];
    
    _codeField = [[UITextField alloc]init];
    _codeField.delegate = self;
    _codeField.font = [UIFont systemFontOfSize:15];
    _codeField.placeholder =@"请输入手机验证码";
    _codeField.frame = CGRectMake(30*kScale, 0, kScreenW-30*kScale, 100*kScale);
    [codeView addSubview:_codeField];
    CGFloat codeBtnW = 100;
    UIButton *codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    codeBtn.frame = CGRectMake(kScreenW-codeBtnW, 0, codeBtnW, 100*kScale);
    //    codeBtn setTitle:@"获取验证码" forState:ui
    codeBtn.tag = 11;
    [codeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    codeBtn.font = [UIFont systemFontOfSize:15];
    [codeBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
    [codeBtn addTarget:self action:@selector(getCode:) forControlEvents:UIControlEventTouchUpInside];
    [codeView addSubview:codeBtn];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(kScreenW-codeBtnW-20*kScale, 10*kScale, 6*kScale, 80*kScale)];
    lineView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0f];
    [codeView addSubview:lineView];
}
- (void)requestWithTag:(NSInteger)tag
{
    
    NSString *getCodeStr = [NSString stringWithFormat:@"%@/sys/login/memberRegistr/sms/randomCode?mobile_no=%@",APIYIGOU,_phoneField.text];
    ;
    NSString *saveStr = [NSString stringWithFormat:@"%@/userCenter/modifyMemberMobile?user_mobile_no=%@&reg_num=%@",APIYIGOU,_phoneField.text,_codeStr];
    NSString *urlStr = nil;
    if (tag==11)
    {
        urlStr = getCodeStr;
    }
    else if (tag==10)
    {
        urlStr = saveStr;
    }
    
    DYBaseRequest *request = [DYBaseRequest new];
    request.requestUrl = urlStr;
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
        NSString *boolSuccess =[NSString stringWithFormat:@"%@", allDic[@"success"]];
        NSLog(@"77777%@",allDic);
        if ([boolSuccess isEqualToString:@"1"])
        {
            
            _codeStr = allDic[@"data"];
            
            
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"验证码发送失败" message:nil delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            [alert show];
        }

    } error:^(YTKBaseRequest *request) {
        
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
    }];
    
}
- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)getCode:(UIButton*)sender
{
   //验证是否是手机号

    [self requestWithTag:sender.tag];


    
}
- (void)saveClick:(UIButton*)sender
{
    if (_codeField.text == _codeStr)
    {
        [self requestWithTag:sender.tag];
        for (UIViewController *temp in self.navigationController.viewControllers) {
            if ([temp isKindOfClass:[SetViewController class]]) {
                [self.navigationController popToViewController:temp animated:YES];
            }
        } ;
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"验证失败" message:@"请输入正确验证码" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil];
        [alert show];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
